package com.per.mybootall.htmlToPdf.newPdf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class WkHtmlToPdf {

    private static final String toPdfTool = "D:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";

    private static final String toPdfCssFangSong = "D:\\AlibabaPuHuiTi\\AlibabaSans-Medium.svg" ;


    /** 主体方法生成  pdf */
    public static void testConvert( String srcPath, String destPath){
       // log.info("css样式在服务器上存的路径[{}]",toPdfCssFangSong);
        String pageSize = "A4";
        File file = new File(destPath);
        File parent = file.getParentFile();
        if (!parent.exists()){
            parent.mkdirs();
        }
        StringBuilder cmd = new StringBuilder();
        cmd.append(toPdfTool).append(" ");//wkhtmltopdf 的安装地址
        cmd.append("--page-size ");
        cmd.append(pageSize).append(" ");
        cmd.append("--margin-top ");
        cmd.append("24mm").append(" ");
        cmd.append("--margin-bottom ");
        cmd.append("15mm").append(" ");

        cmd.append("--user-style-sheet ");
        cmd.append(toPdfCssFangSong).append(" ");//为生成的 pdf 文档设置自定义样式，非常重要！！！
        cmd.append("--disable-smart-shrinking ");//这个参数一定要加上，加上页面就不缩小了，会正常比例显示

        cmd.append(srcPath).append(" ");
        cmd.append(destPath);
        try {
            Runtime.getRuntime().exec(cmd.toString());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {

            //设置纸张大小: A4, Letter, etc.
            String pageSize = "A4";
            //需要生成PDF的URL，这样也可以是页面的本地地址

            String srcPath = "D:\\1615426761-NY0F1ONVI2.html";

            String destPath = "D:\\1.pdf";
            testConvert(srcPath, destPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }






}
